package hcontroller

import (
	"encoding/json"

	"advertoper/controllers"
	"advertoper/errorcode"
	"advertoper/homepageinfo/hservice"
)

type UserInfoController struct {
	controllers.URLSignController
}

//客户端查询实时价格和秒杀剩余数量
type GoodsNowPriceQuery struct {
	controllers.LoginedController
}

//客户端请求数据
// @router /get/data [get]
func (p *UserInfoController) GetHomePageData() {
	defer p.Output("GetHomePageData")
	p.ForceHTTPS()

	appkey := p.GetString("appkey", "")
	plat := 1
	if appkey == "cc0dec8c07444e9dadb75d41218c4fe4" {
		plat = 2
	}

	regionCode := p.GetString("region_code", "all")
	versionCode, _ := p.GetInt("version_code", 0)

	source, _ := p.GetInt("source", 0)

	code, msg, fingerprint, data := hservice.GetUserInfoService().GetHomePageData(regionCode, versionCode, plat, source)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == 0 {
		p.Resp["fingerprint"] = fingerprint
		p.Resp["data"] = data
	}
}

//查询实时商品价格
// @router /zjgoodslist/now-price [post]
func (p *GoodsNowPriceQuery) GoodsListNowPriceQuery() {
	defer p.Output("GoodsListNowPriceQuery")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	data := make(map[string]int)
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	goodsId := data["goods_id"]

	dt := hservice.GetZJGoodsListInfoService().GetZJGoodsItemById(goodsId)
	if dt == nil {
		p.Resp["code"] = 101
		p.Resp["msg"] = "商品不存在"
	} else {
		p.Resp["code"] = 0
		p.Resp["msg"] = "success"
		p.Resp["now_price"] = dt.Price
	}
}

//查询实时秒杀商品价格
// @router /seckill/now-price [post]
func (p *GoodsNowPriceQuery) SeckillNowPriceQuery() {
	defer p.Output("SeckillNowPriceQuery")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	data := make(map[string]int)
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	goodsId := data["seckill_id"]

	dt := hservice.GetZJSeckillInfoService().GetZJSeckillItemById(goodsId)
	if dt == nil {
		p.Resp["code"] = 101
		p.Resp["msg"] = "商品不存在"
	} else {
		p.Resp["code"] = 0
		p.Resp["msg"] = "success"
		p.Resp["now_price"] = dt.Price
	}
}
